﻿Imports GBDZafiro

Public Class FrmBodega

    Private Sub FrmBodega_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        cargargvBodega()
    End Sub

    Protected Overrides Sub bnNuevo_Click(sender As System.Object, e As System.EventArgs)
        MyBase.bnNuevo_Click(sender, e)
        tbBodega.Focus()
    End Sub

    Protected Overrides Sub bnGuardar_Click(sender As System.Object, e As System.EventArgs)
        If Not validarCampos() Then
            Exit Sub
        End If
        updateAddBga()
        MyBase.bnGuardar_Click(sender, e)
        cargargvBodega()
    End Sub

    Private Sub gvBodega_RowHeaderMouseClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles gvBodega.RowHeaderMouseClick
        selectBodega()
    End Sub

    Protected Overrides Sub bnEliminar_Click(sender As System.Object, e As System.EventArgs)
        Dim bga As Bodega = crearBodega()
        Dim result As GBDZafiro.Resultado = GBDZafiro.Bodega.Eliminar(bga)
        If result.ExisteError Then
            msnBox.msnExclamacion(result.Mensaje)
            Exit Sub
        End If
        MyBase.bnEliminar_Click(sender, e)
        cargargvBodega()
    End Sub

    Private Sub gvBodega_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles gvBodega.KeyPress
        ' selectBodega()
    End Sub

    Private Sub gvBodega_CellLeave(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gvBodega.CellLeave
        'selectBodega()
    End Sub

    Private Sub gvBodega_CurrentCellChanged(sender As System.Object, e As System.EventArgs) Handles gvBodega.CurrentCellChanged
        selectBodega()
    End Sub

    Private Sub selectBodega()
        Try
            Dim rowgvbga As New DataGridViewRow()
            rowgvbga = gvBodega.CurrentRow
            tbBodega.Text = rowgvbga.Cells.Item("COD_BODEGA").Value.ToString()
            tbDescripcion.Text = rowgvbga.Cells.Item("DESCRIPCION").Value.ToString()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub updateAddBga()
        Dim bga As Bodega = crearBodega()
        If Bodega.exisBodega(bga) Then
            Bodega.Actualizar(bga)
            'msnBox.msnInformacion("Bodega Actualizada")
        Else
            Bodega.Nuevo(bga)
            'msnBox.msnInformacion("Bodega Guardada")
        End If
    End Sub

    Private Function validarCampos() As Boolean
        If tbBodega.TextLength < 1 Or tbDescripcion.TextLength < 1 Then
            msnBox.msnExclamacion("Complete todos los campos")
            Return False
        Else
            Return True
        End If
    End Function

    Private Function crearBodega() As Bodega
        Dim bga As New Bodega
        bga.codBodega = tbBodega.Text.ToUpper()
        bga.descripcion = tbDescripcion.Text.ToUpper()
        Return bga
    End Function

    Private Sub cargargvBodega()
        gvBodega.DataSource = Nothing
        gvBodega.Rows.Clear()
        gvBodega.DataSource = Bodega.ConsultarAll()
        gvBodega.Columns(0).HeaderText = "BODEGA"
    End Sub
  
End Class
